﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Estimate.Common.DBFieldTypes;
using Estimate.Common.Entity;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace Estimate.Common.Repository
{
    public class SiteForeRepository : ForeRepository
    {

        public static readonly string SemiStations = "58238;58237;58235;58340;58339;58027;58012;58013;58026;58035;58130;58044;58040;58036;58047;58048;58154;58045;58049;58143;58146;58150;58158;58251;58131;58038;58132;58135;58141;58138;58139;58140;58145;58147;58245;58148;58241;58244;58242;58246;58257;58249;58250;58243;58259;58254;58255;58264;58268;58360;58269;58252;58341;58344;58247;58343;58345;58342;58354;58351;58346;58357;58353;58352;58356;58377;58359";
                                                   
        public SiteForeRepository(string connectionString, string tableName)
            : base(connectionString, tableName)
        {

        }

        /// <summary>
        /// 获取所有SiteFore表名
        /// </summary>
        public static List<string> GetSiteForeTables(string connectionString)
        {
            List<string> SiteForeTables = new List<string>();
            string sql = "select name from sysobjects where xtype='U' and name like '%_SiteFore'";

            SqlConnection connection = new SqlConnection(connectionString);
            SqlDataReader reader = DBHelper.ExecuteReader(connection, sql);
             while (reader.Read())
                SiteForeTables.Add(reader["name"].ToString());
            connection.Close();
            return SiteForeTables;

        }


        /// <summary>
        /// 使用站号列表筛选semi
        /// 不同于父类的方法，在这里，不需要考虑将58252做替换的问题
        /// </summary>
        /// <param name="semi">semi字符串</param>
        /// <param name="deststream">站点列表字符串</param>
        /// <returns>相关站点的semi字符串</returns>
        public override string FilterSemiWithStationIds(string semi, string deststream)
        {
            List<string> result = new List<string>();

            //无论是temp还是water,|分隔，其每个小semi前5位都是站号
            string[] ss = Regex.Split(semi, @"\|", RegexOptions.IgnoreCase);
            foreach (string s in ss)
            {
                string station = s;

                if (s.Length < 6)
                    continue;
                if (deststream.IndexOf(station.Substring(0, 5)) != -1)
                    result.Add(s);
            }
            return string.Join("|", result.ToArray());
        }

        /// <summary>
        /// 给定查询条件，获取预测值
        /// </summary>
        /// <param name="foreEntity">数据记录</param>
        /// <returns></returns>
        public override ForeEntity GetForeRecord(DateTime date, ForeClockType clock, ForeSpanType span, ForeSendpointType sendpoint, ForeFactorType type, ForeSemiType semitype)
        {
            ForeEntity entity = base.GetForeRecord(date, clock, span, sendpoint, type, semitype);
            if (entity != null)
            {                
                entity.Semi = FilterSemiWithStationIds(entity.Semi, SemiStations);
            }
            return entity;
        }

        /// <summary>
        /// 给定日期范围及查询条件，获取相应的所有semi值
        /// </summary>
        /// <param name="start">开始日期</param>
        /// <param name="end">结束日期</param>
        public override ForeEntity[] InquerySemiWithDateRange(DateTime start, DateTime end, ForeFactorType type, ForeSemiType semitype)
        {
            ForeEntity[] result = base.InquerySemiWithDateRange(start, end, type, semitype);
            foreach (ForeEntity entity in result)
            {
                entity.Semi = FilterSemiWithStationIds(entity.Semi, SemiStations);
            }
            return result;
        }      

        /// <summary>
        /// 给定日期范围及查询条件，获取相应的所有semi值
        /// </summary>
        /// <param name="start">开始日期</param>
        /// <param name="end">结束日期</param>
        public override ForeEntity[] InquerySemiWithDateRange(DateTime start, DateTime end, ForeClockType[] clocks, ForeSpanType span, ForeSendpointType[] sendpoints, ForeFactorType type, ForeSemiType semitype, bool needCache)
        {
            ForeEntity[] result = base.InquerySemiWithDateRange(start, end, clocks, span, sendpoints, type, semitype, needCache);
            foreach (ForeEntity entity in result)
            {
                entity.Semi = FilterSemiWithStationIds(entity.Semi, SemiStations);
            }
            return result;
        }       
       

        /// <summary>
        /// 当有了实况值后，会根据筛选出 date, clock, span 三个参数。符合这三个参数的记录，是与实况记录相吻合的预报记录
        /// 获取这些预报记录，然后将计算semi
        /// </summary>
        /// <returns>符合条件的预报记录</returns>
        public override ForeEntity[] RealValueFilteForeRecord(DateTime date, ForeClockType clock, ForeSpanType span, ForeFactorType type, ForeSemiType semitype, bool needCache)
        {
            ForeEntity[] result = base.RealValueFilteForeRecord(date, clock, span, type, semitype, needCache);
            foreach (ForeEntity entity in result)
            {
                entity.Semi = FilterSemiWithStationIds(entity.Semi, SemiStations);
            }
            return result;
        }

        /// <summary>
        /// 给定查询条件，获取预测值
        /// </summary>
        /// <param name="foreEntity">数据记录</param>
        /// <returns></returns>
        public override ForeEntity[] GetForeRecordWithDateRange(DateTime beginDate, DateTime endDate, ForeClockType[] clocks, ForeSpanType[] spans, ForeSendpointType[] sendpoints, ForeFactorType[] types, ForeSemiType semitype, bool needCache)
        {
            ForeEntity[] result = base.GetForeRecordWithDateRange(beginDate, endDate, clocks, spans, sendpoints, types, semitype, needCache);
            foreach (ForeEntity entity in result)
            {
                entity.Semi = FilterSemiWithStationIds(entity.Semi, SemiStations);
            }
            return result;
        }

        /// <summary>
        /// 给定查询条件，获取预测值
        /// </summary>
        /// <param name="foreEntity">数据记录</param>
        /// <returns></returns>
        public override ForeEntity[] GetForeRecordWithDate(DateTime select_date, ForeClockType[] clocks, ForeSendpointType[] sendpoints, ForeFactorType factor, ForeSemiType semitype, bool needCache)
        {
            ForeEntity[] result = base.GetForeRecordWithDate(select_date, clocks, sendpoints, factor, semitype, needCache);
            foreach (ForeEntity entity in result)
            {
                entity.Semi = FilterSemiWithStationIds(entity.Semi, SemiStations);
            }
            return result;
        }

        /// <summary>
        /// 给定查询条件，获取预测值
        /// </summary>
        /// <param name="foreEntity">数据记录</param>
        /// <returns></returns>
        public override ForeEntity[] GetForeRecordWithDateRange(DateTime beginDate, DateTime endDate, ForeClockType[] clocks, ForeSendpointType[] sendpoints, ForeSemiType semitype, bool needCache)
        {
            ForeEntity[] result = base.GetForeRecordWithDateRange(beginDate, endDate, clocks, sendpoints, semitype, needCache);
            foreach (ForeEntity entity in result)
            {
                entity.Semi = FilterSemiWithStationIds(entity.Semi, SemiStations);
            }
            return result;
        }

       

    }
}
